//
//  HLineChartView.swift
//  Drop
//
//  Created by hushaohua on 2018/3/24.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit


class HLineChartConfigruation : HLinearChartConfiguration{
    
    var pointSpace:CGFloat?;
    var lineColor:UIColor = UIColor.black;
    var dotColor:UIColor = UIColor.black;
    var showDot:Bool = true;
    var dotRadius:CGFloat = 1;
    var dotColorAlpha:CGFloat = 0.2;
}

class HLineChartView: HLinearChartView {

    override func linearConfiguration() -> HLinearChartConfiguration{
        let config = HLineChartConfigruation();
        config.keyAxisPref = HChartAxisPreference();
        config.valueAxisPref = HChartAxisPreference();
        config.valueAxisPref?.textWidth = 20;
        return config;
    }
    
    func lineConfig() -> HLineChartConfigruation{
        return self.configuration as! HLineChartConfigruation;
    }
    
    var disableKeyTexts:Bool = false;
    override func draw(_ rect: CGRect) {
        self.drawKeyAxis();
        if (!self.disableKeyTexts){
            self.drawKeyTexts();
        }
        self.drawValueAxis();
        self.drawValueTexts();
        self.drawXBaselines();
        self.drawPoints();
    }
    
    override func prepareChartItemsData() {
        super.prepareChartItemsData();
        self.getChartLinePoints();
    }
    
    func keysWidth() -> CGFloat {
        return self.bounds.size.width - self.lineConfig().valueAxisHeight();
    }
    func valuesWidth() -> CGFloat {
        return self.bounds.size.height - self.lineConfig().keyAxisHeight();
    }
    
    func getChartLinePoints() {
        var pointSpace:CGFloat = 0;
        if (self.lineConfig().pointSpace == nil){
            pointSpace = (self.keysWidth()) / CGFloat(self.chartItems!.count + 1);
        }else{
            pointSpace = self.lineConfig().pointSpace!;
        }
        self.getChartPoints(pointSpace);
    }
    
    func getChartPoints(_ pointSpace:CGFloat) {
        let config = self.lineConfig();
        var xOffset:CGFloat = config.valueAxisHeight() + pointSpace;
        let keyTextAttributes:[NSAttributedStringKey:Any]? = config.keyAxisPref?.textAttributes;
        for aChart in self.chartItems! {
            let chart = aChart as! HLineChartItem;
//            chart.point.x = 12;
            chart.point.x = xOffset;
            let height:CGFloat = self.valuesWidth() * CGFloat(chart.value) / CGFloat(self.maxValue);
            chart.point.y = self.valuesWidth() - height;
            
            if (keyTextAttributes != nil){
                let string:NSString = NSString(string: chart.key);
                let size = string.boundingRect(with: CGSize(width:pointSpace + pointSpace, height:config.keyAxisHeight()), options: .usesLineFragmentOrigin, attributes: keyTextAttributes, context: nil);
                chart.keyRect.size.width = size.width;
                chart.keyRect.size.height = size.height;
                chart.keyRect.origin.x = xOffset - pointSpace / 2 + (pointSpace + pointSpace - size.width) / 2;
                chart.keyRect.origin.y = self.valuesWidth() + (config.keyAxisHeight() - size.height) / 2;
            }
            
            xOffset += pointSpace;
        }
    }
    
    func drawPoints(){
        let context:CGContext = UIGraphicsGetCurrentContext()!;
        var points:[CGPoint] = [CGPoint]();
        let lineConfig:HLineChartConfigruation = self.lineConfig();
        lineConfig.lineColor.setStroke();
        context.setLineDash(phase: 0, lengths: []);
        for aChart in self.chartItems! {
            let chart = aChart  as! HLineChartItem;
            if (chart.value > 0){
                points.append(chart.point);
            }else{
                if (points.count > 1){
                    context.setLineDash(phase: 0, lengths: []);
                    context.addLines(between: points);
                    context.drawPath(using: .stroke);
                    points.removeAll();
                }else if (points.count == 1){
                    if (!self.lineConfig().showDot){
                        let point:CGPoint = points.last!;
                        context.setLineDash(phase: 0, lengths: []);
                        context.addEllipse(in: CGRect(x: point.x - lineConfig.dotRadius, y: point.y - lineConfig.dotRadius, width: 2 * lineConfig.dotRadius, height: 2 * lineConfig.dotRadius));
                        lineConfig.dotColor.setFill();
                        context.drawPath(using: .fillStroke);
                    }
                    points.removeAll();
                }
            }
        }
        if (points.count > 1){
            context.setLineDash(phase: 0, lengths: []);
            context.addLines(between: points);
            context.drawPath(using: .stroke);
            points.removeAll();
        }
        
        if (self.lineConfig().showDot){
            for aChart in self.chartItems! {
                let chart = aChart as! HLineChartItem;
                context.addEllipse(in: CGRect(x: chart.point.x - lineConfig.dotRadius, y: chart.point.y - lineConfig.dotRadius, width: 2 * lineConfig.dotRadius, height: 2 * lineConfig.dotRadius));
                if (chart.value > 0){
                    self.lineConfig().lineColor.setStroke();
                    self.lineConfig().dotColor.setFill();
                }else{
                    self.lineConfig().lineColor.withAlphaComponent(lineConfig.dotColorAlpha).setStroke();
                    self.lineConfig().dotColor.withAlphaComponent(lineConfig.dotColorAlpha).setFill();
                }
                context.drawPath(using: .fillStroke);
                
            }
        }
    }

}
